package com.main.taxsystem;

import java.util.ArrayList;
import java.util.HashMap;

import com.main.taxsystem.TableInfoSet.TestDataTableInfo;

import android.app.ActionBar;
import android.app.ListActivity;
import android.app.LoaderManager;
import android.app.SearchManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ListAndSearchActivity extends ListActivity
implements LoaderManager.LoaderCallbacks<Cursor>,SearchView.OnQueryTextListener{
	private static final String TAG = "ListAndSearchActivity";
	private ListView listView = null;
	private ListView resultListView = null;
	private String activityTitle = null;
	private SimpleCursorAdapter adapter = null;
	private SearchView searchView = null;
	private Cursor cursor = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_and_search);
		initActionBar();
        
		activityTitle = getIntent().getStringExtra("title");
		setTitle(activityTitle);
		
        setTitle(getIntent().getStringExtra("title"));
        resultListView = (ListView) this.findViewById(R.id.result_list);
        adapter = new SimpleCursorAdapter(
        		this,
        		R.layout.list_and_search_item,
        		cursor,
        		new String[]{TestDataTableInfo.COLUMN_NAME_TITLE,TestDataTableInfo.COLUMN_NAME_CREATE_TIME},
        		new int[]{R.id.textView1,R.id.textView2},
        		CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(new CustomOnListItemClick());
        getLoaderManager().initLoader(2, null, this);
	}
	
	void initActionBar(){
		ActionBar bar = getActionBar();
		bar.setDisplayHomeAsUpEnabled(true);
        bar.setHomeButtonEnabled(true);
        bar.setIcon(R.drawable.actionbar_icon);
        bar.setBackgroundDrawable(new ColorDrawable(0xff34a4f7)); 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.list_and_search, menu);
		
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
			Log.v(TAG,"onCreateOptionsMenu(): is version Honeycomb");
			SearchManager searchManaer = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
			searchView = (SearchView) menu.findItem(R.id.searchView).getActionView();
			searchView.setSearchableInfo(searchManaer.getSearchableInfo(getComponentName()));
			searchView.setSubmitButtonEnabled(true);
			searchView.setQueryRefinementEnabled(true);
			searchView.setOnQueryTextListener(this);
        }
		return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.searchView:
            	Log.v(TAG,"onOptionsItemSelected(): selected R.id.searchView");
                return false;
            default:
                return false;
        }
    }
	
	
//	@Override
//	public boolean onSearchRequested() {
//		Bundle appData = new Bundle();
//		appData.putString("title", getTitle().toString());
//		startSearch(null, false, appData, false);
//		return true;
//	}



	protected class CustomOnListItemClick implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent = new Intent(ListAndSearchActivity.this,ListItemShowActivity.class);
			intent.putExtra("title", cursor.getString(cursor.getColumnIndex(TestDataTableInfo.COLUMN_NAME_TITLE)));
			startActivity(intent);
		}
		
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		CursorLoader loader = new CursorLoader(this,
				TaxSystemContentProvider.URI_TEST_DATA, null, null,
				null, null);
		return loader;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data){
		if (data.getCount() < 1) {
			Log.v(TAG, "onCreateLoader() cursor.getCount() < 1, return");
			return;
		}
		adapter.swapCursor(data);
		cursor=data;
		data.setNotificationUri(getContentResolver(), TaxSystemContentProvider.URI_PERSIONAL_NOTICES);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		adapter.swapCursor(null);
	}

	@Override
	public boolean onQueryTextSubmit(String query) {
		Log.v(TAG,"onQueryTextSubmit()");
		searchView.setIconified(true);
		Intent intent = new Intent(ListAndSearchActivity.this,SearchableActivity.class);
		startActivity(intent);
		return true;
	}

	@Override
	public boolean onQueryTextChange(String newText) {
		return false;
	}

	 
}
